package com.example.malldemo.controller;

import com.example.malldemo.common.api.CommonPage;
import com.example.malldemo.common.api.CommonResult;
import com.example.malldemo.domain.EsProductRelatedInfo;
import com.example.malldemo.nosql.elasticsearch.document.EsProduct;
import com.example.malldemo.service.EsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/esProduct")
@Api(tags = "EsProductController",description = "搜索商品管理")
public class EsProductController {

    @Resource
    private EsProductService esProductService;

    @ApiOperation(value = "导入所有数据库中的商品到ES")
    @RequestMapping(value = "/importAll",method = RequestMethod.POST)
    public CommonResult<Integer> importAllList() {
        int count = esProductService.importAll();
        return CommonResult.success(count);
    }

    @ApiOperation(value = "根据id删除商品")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public CommonResult<Object> delete(@PathVariable Long id) {
        esProductService.delete(id);
        return CommonResult.success(null);
    }

    @RequestMapping(value = "/delete/batch",method = RequestMethod.GET)
    @ApiOperation(value = "根据ids批量删除商品")
    public CommonResult<Object> delete(@RequestParam("ids") List<Long> ids) {
        esProductService.delete(ids);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "根据id创建商品")
    @RequestMapping(value = "/create/{id}",method = RequestMethod.POST)
    public CommonResult<EsProduct> create(@PathVariable Long id) {
        EsProduct esProduct = esProductService.create(id);
        if (esProduct != null) {
            return CommonResult.success(esProduct);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "根据商品id推荐商品")
    @RequestMapping(value = "/recommend/{id}",method = RequestMethod.GET)
    public CommonResult<CommonPage<EsProduct>> recommend(@PathVariable Long id,
                                                         @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.recommend(id,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

    @ApiOperation(value = "简单搜索")
    @RequestMapping(value = "/search/simple",method = RequestMethod.GET)
    public CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize
    ) {
        Page<EsProduct> esProductPage = esProductService.search(keyword,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

    @ApiOperation(value = "获取搜索的相关品牌，分类及筛选属性")
    @RequestMapping(value = "/search/relate",method = RequestMethod.GET)
    public CommonResult<EsProductRelatedInfo> searchRelatedInfo(@RequestParam(required = false) String keyword) {
        EsProductRelatedInfo esProductRelatedInfo = esProductService.searchRelatedInfo(keyword);
        return CommonResult.success(esProductRelatedInfo);
    }


    @ApiOperation(value = "综合搜索、筛选、排序")
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false) Long brandId,
                                                      @RequestParam(required = false) Long productCategoryId,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                      @RequestParam(required = false, defaultValue = "0") Integer sort
    ) {
        Page<EsProduct> esProductPage = esProductService.search(keyword,brandId,productCategoryId,pageNum,pageSize,sort);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

}
